<?php

namespace App\Http\Controllers\Admin\Superadmin;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{	

	/*
		用户添加操作
	*/
	public function getAdd(){
		return view('admin.superadmin.user.add');
	}	
   /*
		执行数据的插入
   */
	public function postInsert(request $request){
		//执行检测数据
		//if(!$request->input('username')){
		//	return back()->with('error','用户名未填写');
		//}

		//数据验证  自动闪存
		 $this->validate($request, [
        	'name' => 'required|max:25',
        	'password' => 'required',
        	'make' => 'required|integer|between:0,2',
        
    	],[
    		'name.required'=>'用户名不能为空',
    		'name.max'=>'用户名太长啦',
    		'password.required'=>'密码不能为空',
    		'make.required'=>'状态不能为空',
    		'make.integer'=>'必须是整型数字',
    		'make.between:min,max'=>'必须在0到2之间',
    		

    	]);


		//上面数据全部验证成功
		//提取部分数据
		$data = $request->only(['name','password','make']);
		//添加token 随机字符串 带着id发送到邮箱 通过邮箱点击回到页面
		$data['token'] = str_random(50);
		//对密码进行加密 hash::make
		$data['password'] = Hash::make($data['password']);

		//执行数据插入操作
		$res = DB::table('hs_member')->insert($data);

		//判断添加是否成功
		if($res){
			return redirect('/admin/superadmin/user/index')->with('success','用户添加成功');
		}else{
			return back()->with('error','用户添加失败');	
		}
	}	






    /*
		用户列表页
    */
	public function getIndex(request $request){
		
		//获取分页大小
		$num = $request->input('num',5);
		if($request->input('keywords')){
			$users = DB::table('hs_member')->where('name','like','%'.$request->input('keywords').'%')->paginate($num);
		}else{
			$users = DB::table('hs_member')->paginate($num);
		}

		//提取请求数据
		$data = $request->all();

		//解析模板
		return view('admin.superadmin.user.index',['user'=>$users,'request'=>$data]);
	}

	/*
	===============================	用户屏蔽 shielding =======================
	*/
	public function getShielding($id){
		$data['state'] = 2;
		$res = DB::table('hs_member')->where('id',$id)->update($data);
		//dd($res);
		if($res){
			return redirect('/admin/superadmin/user/index')->with('success','屏蔽成功');
		}else{
			return redirect('/admin/superadmin/user/index')->with('error','屏蔽失败');
		}
		
	}	
	
	/*
		修改表单显示
	*/	
	public function getEdit($id){
		$user = DB::table('hs_member')->where('id',$id)->first();

		//解析模板 显示用户数据
		return view('admin.superadmin.user.edit',['user'=>$user]);
	}	

	/*
		用户数据修改
	*/
	public function postUpdate(request $request){
		//数据验证  自动闪存
		 $this->validate($request, [
        	'name' => 'required|max:25',    	
        	'email' => 'required|email',
        	'make' => 'required|integer|between:0,2',

        	
    	],[
    		'name.required'=>'用户名不能为空',
    		'name.max'=>'用户名太长啦',
    		'email.required'=>'邮箱不能为空',
    		'email.email'=>'邮箱格式不正确',
    		'make.required'=>'状态不能为空',
    		'make.integer'=>'必须是整型数字',
    		'make.between:min,max'=>'必须在0到2之间',
    	]);			


		//获取id
		$id = $request->only(['id']);
		//详细数据
		$data = $request->only(['name','email','make']);
		//执行修改
		$res = DB::table('hs_member')->where('id',$id)->update($data);
		//判断是否添加成功
		if($res){
			return redirect('/admin/superadmin/user/index')->with('success','用户修改成功');
		}else{
			return back()->with('error','用户修改失败');
		}
	}	
 
	
}	
